package com.example.cofeed.activity;

import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.text.TextUtils;
import android.view.View;
import android.widget.EditText;
import android.widget.TextView;

import com.blankj.utilcode.util.SPUtils;
import com.example.cofeed.R;
import com.example.cofeed.api.ApiService;
import com.example.cofeed.base.BaseActivity;
import com.example.cofeed.callback.DialogCallback;
import com.example.cofeed.callback.LzyResponse;
import com.example.cofeed.constant.IntentKeyConstant;
import com.example.cofeed.constant.SputilsConstant;
import com.lzy.okgo.model.Response;
import com.umeng.analytics.MobclickAgent;

import butterknife.BindView;
import butterknife.OnClick;

public class ForgetPwdActivity extends BaseActivity {

    @BindView(R.id.et_code)
    EditText mEtCode;
    @BindView(R.id.tv_get_code)
    TextView mTvGetCode;
    //    @BindView(R.id.et_name)
//    EditText mEtName;
    @BindView(R.id.et_pwd)
    EditText mEtPwd;
    @BindView(R.id.et_again_pwd)
    EditText mEtAgainPwd;
    @BindView(R.id.confirm_tv)
    TextView mConfirmTv;
    private String mPhone;
    private int mAccountId;
    private int mType;

    @Override
    protected int getLayoutId() {
        return R.layout.activity_forget_pwd;
    }

    @Override
    protected void init(Bundle savedInstanceState, View container) {
        setMainTitle("密码修改");
        mPhone = getIntent().getStringExtra(IntentKeyConstant.PHONE);
        mAccountId = getIntent().getIntExtra(IntentKeyConstant.ACCOUNT_ID, 0);
        mType = getIntent().getIntExtra(IntentKeyConstant.TYPE, 0);

        getCode();

    }

    @OnClick({R.id.tv_get_code, R.id.confirm_tv})
    public void onViewClicked(View view) {
        switch (view.getId()) {
            case R.id.tv_get_code:
                getCode();
                break;
            case R.id.confirm_tv:
                String code = mEtCode.getText().toString().trim();
//                String name = mEtName.getText().toString().trim();
                String pwd = mEtPwd.getText().toString().trim();
                String pwdAgain = mEtAgainPwd.getText().toString().trim();
                if (TextUtils.isEmpty(code)) {
                    showToast("请输入验证码");
                    return;
                }
//                if (TextUtils.isEmpty(name)){
//                    showToast("请输入用户名");
//                    return;
//                }
                if (TextUtils.isEmpty(pwd)) {
                    showToast("请输入密码");
                    return;
                }
                if (TextUtils.isEmpty(pwdAgain)) {
                    showToast("请再次输入密码");
                    return;
                }
                if (!pwd.equals(pwdAgain)) {
                    showToast("两次密码不一致");
                    return;
                }

                ApiService.password_check(this, pwd, mAccountId, code, new DialogCallback<LzyResponse<Object>>() {
                    @Override
                    public void onSuccess(Response<LzyResponse<Object>> response) {
                        showToast(response.body().message);

                        new Handler().postDelayed(new Runnable() {
                            @Override
                            public void run() {
                                if (mType > 0) {
                                    SPUtils.getInstance().put(SputilsConstant.TOKEN, "");
                                    SPUtils.getInstance().put(SputilsConstant.AUTO_TOKEN, "");
                                    Intent intent = new Intent(mContext, LoginActivity.class);
                                    intent.addFlags(Intent.FLAG_ACTIVITY_NEW_TASK | Intent.FLAG_ACTIVITY_CLEAR_TASK);
                                    startActivity(intent);
                                }else {
                                    finish();
                                }

                            }
                        }, 3000);
                    }
                });

                break;
            default:
        }
    }


    private void getCode() {
        ApiService.verifycode(this, mPhone, 2, new DialogCallback<LzyResponse<Object>>(this) {
            @Override
            public void onSuccess(Response<LzyResponse<Object>> response) {
                mTvGetCode.setClickable(false);
                mTvGetCode.setText(expired_time + "秒");
                mHandler.postDelayed(runnable, 1000);

            }
        });

    }


    private Handler mHandler = new Handler();
    private int expired_time = 60;

    /**
     * 60秒倒计时
     */
    private Runnable runnable = new Runnable() {

        @Override
        public void run() {
            expired_time--;
            mTvGetCode.setText(expired_time + "秒");
            if (expired_time == 0) {
                initVcode();
                return;
            }
            mHandler.postDelayed(this, 1000);
        }
    };

    /*
     * 60秒过后初始化各状态
     */
    private void initVcode() {
        expired_time = 60;
        mTvGetCode.setClickable(true);
        mTvGetCode.setText("获取验证码");
    }

    @Override
    public void onResume() {
        super.onResume();
        MobclickAgent.onResume(this);
    }

    @Override
    public void onPause() {
        super.onPause();
        MobclickAgent.onPause(this);
    }

}
